require("utils")
require("tier2")
require("gems") -- gems.lua
require("enchants")
require("eq") -- eq.lua
require("buffs") -- buffs.lua
require("races") -- races.lua

local FSR_RATIO = 0.8

local unit = {
	race = "NIGHTELF",
	class = "DRUID",
	level = 70,
	innate = {
		strength = 73,
		intellect = 120,
		agility = 75,
		stamina = 82,
		spirit = 132,
		skill_defense = 350,
		skill_weapon_feral = 350,
		armor = 0,
		maxhealth = 3614,
		maxmana = 2370,
		meleeattackpower = -20,
	},
	auras = {
		-- Talents
		{ type = "modifier", attribute = "spirit", value = 15 },
		{ type = "stat", attribute = "spellcrit", value = 3 },
		{ type = "stat", attribute = "manareg_fsr_p", value = 0.30 },
		-- Tree of Life form:
		{ type = "modifier", attribute = "healing", reference = "spirit", multiplier = 0.25 },
		-- Night Elf
		{ type = "stat", attribute = "dodge", value = 1 },
		-- Dire Bear form:
--~ 		{ type = "modifier", attribute = "base_armor", value = 400 },
--~ 		{ type = "modifier", attribute = "stamina", value = 25 },
--~ 		{ type = "stat", attribute = "meleeattackpower", value = 120 },
	},
}

local heal_eq = {
	head = {
		item = eq.head.t6,
		socket1 = gems.blue.spi10,
		socket2 = gems.meta.intemana,
		enchant = enchants.head.heal_hh,
	},
	neck = {
		item = eq.neck.nether,
	},
	shoulders = {
		item = eq.shoulders.t6,
		socket1 = gems.blue.spi10,
		socket2 = gems.blue.spi10,
		enchant = enchants.shoulders.aldor_exalted,
	},
	back = {
		item = eq.back.prince,
	},
	chest = {
		item = eq.chest.badge_hauthaa,
		socket1 = gems.red.heal22,
		enchant = enchants.chest.spirit,
	},
	wrists = {
		item = eq.wrists.rage,
		socket1 = gems.purple.pure_pearl,
		enchant = enchants.wrists.healing,
	},
	mainhand = {
		item = eq.mainhand.solarian,
		enchant = enchants.mainhand.spirit,
	},
	hands = {
		item = eq.hands.t6,
		socket1 = gems.blue.spi10,
		enchant = enchants.hands.healing,
	},
	waist = {
		item = eq.waist.za_timer,
		socket1 = gems.orange.heal11int5,
		socket2 = gems.purple.heal11spi5,
	},
	legs = {
		item = eq.legs.badge_hauthaa,
		socket1 = gems.blue.spi10,
		socket2 = gems.orange.lum_noble_topaz,
		enchant = enchants.legs.aldor_exalted,
	},
	feet = {
		item = eq.feet.anetheron,
		enchant = enchants.feet.spirit,
	},
	finger1 = {
		item = eq.finger.hyjal3,
	},
	finger2 = {
		item = eq.finger.karazhan_exalted,
	},
	trinket1 = {
		item = eq.trinket.bwl_rejuv,
	},
	trinket2 = {
		item = eq.trinket.kara_aran,
	},
}

local heal2_eq = utils.clone(heal_eq)

heal2_eq.chest = { -- T6 7093.2
	item = eq.chest.t6,
	socket1 = gems.orange.heal11int5,
	socket2 = gems.blue.spi10,
	socket3 = gems.blue.spi10,
	enchant = enchants.chest.spirit,
}

--~ heal2_eq.legs = { -- T6 329.9
--~ 	item = eq.legs.t6,
--~ 	socket1 = gems.blue.spi10,
--~ 	enchant = enchants.legs.aldor_exalted,
--~ }

heal2_eq.back = { -- Kael'Thas 4200.7 / Gurtogg 2893.4
	item = eq.back.gurtogg,
--~ 	item = eq.back.kaelthas,
}

--~ heal2_eq.chest = { -- T5 289.7
--~ 	item = eq.chest.t5,
--~ 	socket1 = gems.purple.pure_pearl,
--~ 	socket2 = gems.purple.pure_pearl,
--~ 	socket3 = gems.purple.pure_pearl,
--~ 	enchant = enchants.chest.spirit,
--~ }

--~ heal2_eq.shoulders = { -- T6 261.3
--~ 	item = eq.shoulders.t6,
--~ 	socket1 = gems.purple.pure_pearl,
--~ 	socket2 = gems.purple.pure_pearl,
--~ 	enchant = enchants.shoulders.aldor_exalted,
--~ }

heal2_eq.finger1 = {
	item = eq.finger.sunwell, -- 7585
--~ 	item = eq.finger.vashj, -- Vashj 2334
--~ 	item = eq.finger.hyjal2, -- 34.5
--~ 	item = eq.finger.hyjal3, -- 80.8
}

heal2_eq.finger2 = {
	item = eq.finger.sunwell, -- 6343.7
}

--~ heal2_eq.head = { -- T6  158.7
--~ 	item = eq.head.t6,
--~ 	socket1 = gems.purple.pure_pearl,
--~ 	socket2 = gems.meta.healing,
--~ 	enchant = enchants.head.heal_hh,
--~ }

--~ heal2_eq.shoulders = { -- T5 64.3
--~ 	item = eq.shoulders.t5,
--~ 	socket1 = gems.purple.pure_pearl,
--~ 	socket2 = gems.purple.pure_pearl,
--~ 	enchant = enchants.shoulders.aldor_exalted,
--~ }

--~ heal2_eq.head = { -- T5  21
--~ 	item = eq.head.t5,
--~ 	socket1 = gems.meta.healing,
--~ 	socket2 = gems.orange.lum_noble_topaz,
--~ 	enchant = enchants.head.heal_hh,
--~ }

local b = {
	buffs.mark_of_the_wild,
	buffs.fortitude,
	buffs.arcane_brilliance,
	buffs.spirit,
	buffs.well_fed_healspirit,
	buffs.blessing_kings,
	buffs.wisdom_elixir,
--~ 	buffs.flask_regen,
--~ 	buffs.flask_intellect,

--~ 	buffs.winterveil_spirit,
}

local function main ()
	local unit_2 = utils.clone(unit)

	utils.InsertAuras(unit, utils.GetEquipmentAuras(heal_eq))
	utils.InsertAuras(unit, b)

	utils.InsertAuras(unit_2, utils.GetEquipmentAuras(heal2_eq))
	utils.InsertAuras(unit_2, b)

	tier2.CALCULATORS.innervate = function (unit, tier1)
		local manareg = tier2.get(unit, tier1, "manareg")
		return manareg * (1 + 400 / 100) * (2 / 5) * (18 / 2)
	end
	tier1.EMPTY.innervate = 0
	tier1.EMPTY.mreg = 0

	tier2.CALCULATORS.mreg = function (unit, t1)
		return (tier2.get(unit, t1, "manareg_fsr") * FSR_RATIO + tier2.get(unit, t1, "manareg") * (1 - FSR_RATIO)) / 5
	end

	tier2.CALCULATORS.lifebloom_healing_power = function (unit, t1)
		local healing = tier2.get(unit, t1, "healing")
		local mana = tier2.get(unit, t1, "maxmana") + tier2.get(unit, t1, "innervate")
		local manareg = (tier2.get(unit, t1, "manareg_fsr") * FSR_RATIO + tier2.get(unit, t1, "manareg") * (1 - FSR_RATIO)) / 5

		local crit = tier2.get(unit, t1, "spellcrit") / 100
		crit = 1 + (crit / 100) / 2
		local lifebloom_healing = ((healing * 0.46 + 273) + (healing * 0.46 + 600)) * crit
		local lifebloom_time = 1.5
		local lifebloom_cost = math.max(1, 220 * 0.8 - manareg * lifebloom_time)
		local lifebloom_casts = mana / lifebloom_cost
		local lifebloom_heal = lifebloom_healing * lifebloom_casts

--~ 		print(
--~ 			string.format("%.1f", lifebloom_heal),
--~ 			string.format("%.1f", lifebloom_cost),
--~ 			string.format("%.1f", lifebloom_casts),
--~ 			string.format("%.1f", lifebloom_healing)
--~ 		)
		return lifebloom_heal
	end
	tier1.EMPTY.lifebloom_healing_power = 0


	local v1, v2 = tier2.calc(unit), tier2.calc(unit_2)

	for s, v in pairs(v1) do
		if v ~= 0 or v2[s] ~= 0 then
			local diff = v2[s] - v
			if diff > 0 then
				diff = ("+%.1f"):format(diff)
			elseif diff < 0 then
				diff = ("-%.1f"):format(-diff)
			else
				diff = nil -- ""
			end
--~ 			if diff then print(s, ("%.2f"):format(v1[s]), diff) end
		end
	end

	local a, b = v2.lifebloom_healing_power - v1.lifebloom_healing_power, v2.spirit - v1.spirit
--~ 	print(v2.lifebloom_healing_power, v1.lifebloom_healing_power)
--~ 	print(string.format("%.1f", a * 0.001 + b * 0.1), v2.spirit, v2.manareg, v2.manareg_fsr, v2.innervate)
	print(
		string.format("Esprit: %d", math.floor(v2.spirit + .5)),
		string.format("Valeur: %.1f", a * 0.001 + b * 0.1),
		string.format("DSpi  : %.1f", b),
		string.format("DHeal : %.1f", v2.healing - v1.healing),
		string.format("Dmreg : %.1f", v2.mreg - v1.mreg)
	)
--	print(v1.spirit, v1.intellect, v1.manareg, v1.manareg_fsr, v1.innervate)
	print(string.format("Une innervation rend %.1f%% / %.1f%% de la mana max", v1.innervate / v1.maxmana * 100, v2.innervate / v2.maxmana * 100))
end

main()
